package com.axc.bug.controller;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.OutputStream;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.axc.common.utils.*;
import com.axc.system.domain.UserDO;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.axc.bug.domain.QuestionInfoDO;
import com.axc.bug.service.QuestionInfoService;
import com.axc.common.annotation.Log;
import com.axc.base.controller.BaseController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 项目表
 * 
 * @author admin
 * @email yfq900108@163.COM
 * @date 2019-07-23 14:53:18
 */
 
@Controller
@RequestMapping("/bug/questionInfo")
public class QuestionInfoController extends BaseController {

	//添加日志模块
	private final Logger logger = LoggerFactory.getLogger(this.getClass());
	
	@Autowired
	private QuestionInfoService questionInfoService;

	//region 视图
	@GetMapping()
	@RequiresPermissions("bug:questionInfo:questionInfo")
	String QuestionInfo(){
	    return "bug/questionInfo/questionInfo";
	}
	

	
	
	/**
	 * 新增页面，无需传值
	 */
	@Log("请求新增页面")
	@GetMapping("/add")
	@RequiresPermissions("bug:questionInfo:add")
	String add(){
	    return "bug/questionInfo/add";
	}


	@Log("请求编辑页面")
	@GetMapping("/edit/{id}")
	@RequiresPermissions("bug:questionInfo:edit")
	String edit(@PathVariable("id") String id,Model model){
		QuestionInfoDO questionInfo = questionInfoService.get(id);
		model.addAttribute("questionInfo", questionInfo);
	    return "bug/questionInfo/edit";
	}

	@Log("请求编辑页面")
	@GetMapping("/view/{id}")
	@RequiresPermissions("bug:questionInfo:edit")
	String view(@PathVariable("id") String id,Model model){
		QuestionInfoDO questionInfo = questionInfoService.get(id);
		model.addAttribute("questionInfo", questionInfo);
		return "bug/questionInfo/view";
	}


	/**
	 * 新增页面
	 */
	@Log("请求新增页面带参数 ")
	@GetMapping("/addModel")
	@RequiresPermissions("bug:questionInfo:addModel")
	String addModel(){
		return "bug/questionInfo/addModel";
	}

	/**
	 * 编辑页面
	 */
	@Log("请求编辑页面带参数 ")
	@GetMapping("/editModel")
	@RequiresPermissions("bug:questionInfo:editModel")
	String editModel(){
		return "bug/questionInfo/editModel";
	}

	/**
         * 列表页面
         */
	@Log("请求列表页面 ")
	@GetMapping("/listPager")
	@RequiresPermissions("bug:questionInfo:listPager")
	String listPager(){
		return "bug/questionInfo/listPager";
	}

	//endregion

	//region 获取数据
	@Log("请求访问主页")
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("bug:questionInfo:questionInfo")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//查询列表数据
		Query query = new Query(params);
		List<QuestionInfoDO> questionInfoList = questionInfoService.list(query);
		int total = questionInfoService.count(query);
		PageUtils pageUtils = new PageUtils(questionInfoList, total);
		return pageUtils;
	}

	/**
	 * LayUi 分页查询
	 */
	@Log("请求LayUi页面 ")
	@ResponseBody
	@GetMapping("/PageLayUi_List_By_Query")
	@RequiresPermissions("bug:questionInfo:PageLayUi_List_By_Query")
	public PageLayUi<QuestionInfoDO> PageLayUi_List_By_Query(QuestionInfoDO query){

		List<QuestionInfoDO> dataList = questionInfoService.PageLayUi_List_By_Query(query);
		int total = questionInfoService.total(query);
		PageLayUi<QuestionInfoDO> pageLayUi= new PageLayUi<QuestionInfoDO>(0,"",total,dataList);
		return pageLayUi;
	}

	/**
	 * 根据query ，查询并返回一条记录
	 */
	@Log("请求查询页面，传参 ")
	@ResponseBody
	@GetMapping("/Get_Model_By_Query")
	@RequiresPermissions("bug:questionInfo:Get_Model_By_Query")
	public QuestionInfoDO Get_Model_By_Query(QuestionInfoDO query){
			QuestionInfoDO model = questionInfoService.Get_Model_By_Query(query);
		return model;
	}


	/**
	 * 导出
	 */
	@RequestMapping(value = "/exportExcel")
	public void exportExcel(@RequestParam Map<String, Object> params, HttpServletRequest request,
							HttpServletResponse response) throws Exception {



		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		String filename = "测试导出功能"+format.format(new Date().getTime())+".xls";
		response.setContentType("application/ms-excel;charset=UTF-8");
		response.setHeader("Content-Disposition", "attachment;filename="+new String(filename.getBytes(),"iso-8859-1"));
		OutputStream out = response.getOutputStream();
		try {
			Query query = new Query(params);
			String type = request.getParameter("type");
			//导出当前页面数据
			if(type.equals("1")){
				List<QuestionInfoDO> questionInfoList = questionInfoService.list(query);
				ExcelExportUtilYFQ.exportToFile(questionInfoList,out);
			}
			//导出全部数据
			if(type.equals("2")){
				List<QuestionInfoDO> questionInfoList = questionInfoService.list(null);
				// List<XXXDO> XxxDOs = XxxService.list(null);
				ExcelExportUtilYFQ.exportToFile(questionInfoList,out);
			}
			//导出符合条件的全部数据
			if(type.equals("3")){
				query.remove("offset");
				query.remove("limit");
				List<QuestionInfoDO> questionInfoList = questionInfoService.list(query);
				//List<XXXDO> XxxDOs = XxxService.list(query);
				ExcelExportUtilYFQ.exportToFile(questionInfoList,out);
			}
		} catch (Exception e) {
			e.printStackTrace();
			//logger.info("exportExcel出错"+e.getMessage());
		}finally{
			out.close();
		}
	}

	@RequestMapping("/download")
	public void download(HttpServletRequest request, HttpServletResponse response) {
		BufferedInputStream dis = null;
		BufferedOutputStream fos = null;

		String urlString = request.getParameter("urlString");
		String fileName = urlString.substring(urlString.lastIndexOf('/') + 1);

		try {

			URL url = new URL(urlString);
			//response.setContentType("application/x-msdownload;");
			response.setContentType("application/octet-stream");
			response.setHeader("Content-disposition", "attachment; filename=" + new String(fileName.getBytes("utf-8"), "ISO8859-1"));
			response.setHeader("Content-Length", String.valueOf(url.openConnection().getContentLength()));

			dis = new BufferedInputStream(url.openStream());
			fos = new BufferedOutputStream(response.getOutputStream());

			byte[] buff = new byte[2048];
			int bytesRead;
			while (-1 != (bytesRead = dis.read(buff, 0, buff.length))) {
				fos.write(buff, 0, bytesRead);
			}

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (dis != null)
				try {
					dis.close();
				} catch (Exception e) {
					e.printStackTrace();
				}
			if (fos != null)
				try {
					fos.close();
				} catch (Exception e) {
					e.printStackTrace();
				}

		}
	}


	//endregion

	//region 提交数据
	/**
	 * 保存
	 */
	@Log("新增保存操作")
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("bug:questionInfo:add")
	public R save( QuestionInfoDO questionInfo){
		questionInfo.setCreateuser(getUsername());
		questionInfo.setCreatetime(new Date());
		questionInfo.setUpdateuser(getUsername());
		questionInfo.setUpdatetime(new Date());
		if(questionInfoService.save(questionInfo)>0){
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@Log("修改保存操作")
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("bug:questionInfo:edit")
	public R update( QuestionInfoDO questionInfo){
		questionInfo.setUpdateuser(getUsername());
		questionInfo.setUpdatetime(new Date());
		questionInfoService.update(questionInfo);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@Log("列表删除页面数据")
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("bug:questionInfo:remove")
	public R remove(String id){
		if(questionInfoService.remove(id)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@Log("按钮删除页面数据")
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("bug:questionInfo:batchRemove")
	public R remove(@RequestParam("ids[]") String[] ids){
		questionInfoService.batchRemove(ids);
		return R.ok();
	}


	//endregion
	

	
}
